/**
 * BrokerModelImpl.java
 * The Broker Tool Decoupled Model; Pass the Required DAO via the constructor
 *
 * @author:  John Thomason
 *  Course:  CS223
 *  Assign:  Mid Term Exam
 * Seminar:  3
 *    Date:  Jan 26, 2009
 */

// Assigned Package
package trader.db;

// Package Imports
import trader.*;
import java.util.*;

/**
 * BrokerModelImpl Class
 */
public class BrokerModelImpl implements BrokerModel{
    
    ArrayList<BrokerView> changeListeners = new ArrayList<BrokerView>(10);
    BrokerDAO brokerDAO;
    
    /** Creates a new instance of BrokerModelImpl */
    public BrokerModelImpl(BrokerDAO brokerDAO){
        this.brokerDAO=brokerDAO;
    }
    
    /**
     * Adds requester to the list of objects to be notified when an
     * object(Customer, Portfolio or Stock) in the broker model
     * alters state
     * @param bv
     * @throws trader.BrokerException
     */
    public void addChangeListener(BrokerView bv)
    throws BrokerException {
        changeListeners.add(bv);
    }//addChangeListener()
    
    
    /**
     * This method notifies all registered BrokerView listeners
     * that a customer object has changed.
     *
     * @param cust A customer object
     */
    private void fireModelChangeEvent(Customer cust) {
        for (BrokerView v: changeListeners) {
            try {
                v.handleCustomerChange(cust);
            }//try
            catch(Exception e) {
                e.printStackTrace();
            }//catch
        }//for
    }//fireModelChangeEvent()


    /**
     * Adds the Customer to the broker model
     *
     * @param cust A Customer Object to add to the DB
     * @throws trader.BrokerException
     */
    public void addCustomer(Customer cust)
    throws BrokerException {
        try {
            brokerDAO.createCustomer(cust);
            fireModelChangeEvent(cust);
        }//try
        catch (Exception e) {
            throw new BrokerException("BrokerModelImpl.addCustomer\n" + e);
        }//catch
    }//addCustomer()


    /**
     * deletes the customer from the broker model
     *
     * @param cust  The Customer Object to delete
     * @throws trader.BrokerException
     */
    public void deleteCustomer(Customer cust)
    throws BrokerException{
        try {
            brokerDAO.removeCustomer(cust);
            fireModelChangeEvent(cust);
        }//try
        catch (Exception e) {
            throw new BrokerException("BrokerModelImpl.deleteCustomer\n" + e);
        }//catch
    }//deleteCustomer()
    
    
    /**
     * Updates the customer in the broker model
     *
     * @param cust The Customer Object to Update
     * @throws trader.BrokerException
     */
    public void updateCustomer(Customer cust)
    throws BrokerException {
        try {
            brokerDAO.changeCustomerInfo(cust);
            fireModelChangeEvent(cust);
        }//try
        catch (Exception e) {
            throw new BrokerException("BrokerModelImpl.updateCustomer\n" + e);
        }//catch
    }//updateCustomer()

    
    /**
     * Given an id, returns the Customer from the model
     *
     * @param id
     * @return
     * @throws trader.BrokerException
     */
    public Customer getCustomer(String id)
    throws BrokerException {
        Customer cr = null;
        try {
            cr = brokerDAO.getCustomer(id);
        }//try
        catch (Exception e) {
            throw new BrokerException("BrokerModelImpl.getCustomer\n" + e);
        }//catch
        return cr;
    }//getCustomer()
    
    
    /**
     * Returns all customers in the broker model
     *
     * @return
     * @throws trader.BrokerException
     */
    public Customer[] getAllCustomers()
    throws BrokerException {
        Customer[] all = null;
        try {
            all = brokerDAO.getAllCustomers();
        }//try
        catch (Exception e) {
            throw new BrokerException("BrokerModelImpl.getAllCustomers\n" + e);
        }//catch
        return all;
    }//getAllCustomers()

}//BrokerModelImpl Class
